package com.forum.service.impl;


import com.forum.constant.StatusConstant;
import com.forum.mapper.UserMapper;
import com.forum.service.ReportService;
import com.forum.vo.FeedBackVO;
import com.forum.vo.NewUserVO;
import com.forum.vo.SexRatioVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public NewUserVO NewUser(LocalDate begin, LocalDate end) {
        // 计算出日期
        ArrayList<LocalDate> dataList = new ArrayList<>();
        dataList.add(begin);
        // 如果开始时间不等于结束时间，则将开始时间加1天
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dataList.add(begin);
        }
        // 算出每天人数的新增
        ArrayList<Long> userList = new ArrayList<>();
        for (LocalDate date : dataList){
            // 每天最小和最大时间
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", StatusConstant.ENABLE);
            Long count =userMapper.sumByMap(map);
            if (count == null){
                count = 0L;
            }
            userList.add(count);
        }
        return NewUserVO.builder()
                .dataList(StringUtils.join(dataList,","))
                .userList(StringUtils.join(userList,","))
                .build();
    }

    @Override
    public SexRatioVO sexRatio() {
        ArrayList sexList = new ArrayList();
        ArrayList countList = new ArrayList();
        for (int i = 0;i<=1;i++){
            sexList.add(i);
            Long sexNumber = userMapper.sexRatio(i);
            countList.add(sexNumber);
        }
        log.info("性别分类 ，{}", sexList);
        log.info("性别分类的数量 ，{}", countList);
        return SexRatioVO.builder()
                .sex(StringUtils.join(sexList,","))
                .countList(StringUtils.join(countList,","))
                .build();
    }

    @Override
    public FeedBackVO feedBack(LocalDate begin, LocalDate end) {
        //TODO
        return null;
    }
}
